home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: my atoi function, could someone suggest...
- Date: Mon, 15 Jan 96 15:30:01 GMT
- Organization: none
- Message-ID: <821719801snz@genesis.demon.co.uk>
- References: <4cf7ap$q4u@kaleka.seanet.com> <4cq937$if9@ns.RezoNet.NET> <4d6v51$qoh@gryphon.phoenix.net> <821573157snz@genesis.demon.co.uk> <4dbjsf$gmt@ns.RezoNet.NET>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4dbjsf$gmt@ns.RezoNet.NET> ray@ultimate-tech.com "Ray Dunn" writes:
-
- >In referenced article, Lawrence Kirby says...
- >> unsigned value = 0;
- >> unsigned digit = *str - '0';
- >>
- >> if (digit < 10) {
- >> value = digit;
- >
- >This is just a matter of personal style, but using the fact that an
- >unsigned number minus a larger number results in a large unsigned
- >number (so that "digit < 10" is false when *str is less than '0'), is
- >rather obtuse IMO.
-
- It is sometimes interesting to present alternatives and discuss their
- merits afterwards. I did stress that the code was simply intended to show
- alternative approaches. As for being 'obtuse' I'd say that the only
- reason that, say,
-
- if ((ch = getchar()) != EOF)
-
- isn't obtuse is because it is a commonly encountered idiom. If people are
- familiar with the unsigned test above it ceases to be obtuse (and to, say,
- some programmers with machine code experience it may seem completely
- natural). Whether it is sensible code to thrust onto the great unwashed masses
- of C programmers is certainly a debatable point! :-)) I think C programmers
- should have a good feel for unsigned integer types and examples like this
- help with that. As an aside a portable macro implementation of isdigit()
- which may be the most efficient on some systems is:
-
- #define isdigit(ch) ((unsigned)+(ch) - '0' < 10)
-
- In this approach ch is only evaluated once so the macr doesn't suffer
- fromt side-effect problems. The unary + allows the compiler to diagnose
- illegal arguments such as pointers which would be accepted by the cast
- alone.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-